﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using quanlycd3lop.GUI;
using quanlycd3lop.Data;

namespace quanlycd3lop.BL
{
    class cl_ThueDia
    {
        AccessData con;
        Form frm;
        DataTable tb;
        public cl_ThueDia (frm_ThueDia f)
        {
            con = new AccessData();
            frm = f;
        }
        public void refesh()
        {
            ((DataGridView)frm.Controls["DataGridView1"]).DataSource = tb;
            ((ComboBox)frm.Controls["cbx_Macd"]).DataBindings.Clear();
            ((ComboBox)frm.Controls["cbx_Makh"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_Slgmuon"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_Ngaymuon"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_Ngaytra"]).DataBindings.Clear();

            ((ComboBox)frm.Controls["cbx_Makh"]).DataBindings.Add("Text",tb,"MaKH");
            ((ComboBox)frm.Controls["cbx_Macd"]).DataBindings.Add("Text", tb, "MaCD");
            ((TextBox)frm.Controls["txt_Slgmuon"]).DataBindings.Add("Text", tb, "SoLuongmuon");
            ((TextBox)frm.Controls["txt_Ngaymuon"]).DataBindings.Add("Text", tb, "NgayMuon");
            ((TextBox)frm.Controls["txt_Ngaytra"]).DataBindings.Add("Text", tb, "Ngaytra");
            
        }
        public void hienthi(string MaCD,string MaKH)
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[2];
            if (MaCD != null && MaKH != null)
            {
                pr[0] = new SqlParameter("@MaCD", MaCD);
                pr[1] = new SqlParameter("@MaKH", MaKH);
            }
            else
            {
                pr[0] = new SqlParameter("@MaCD", DBNull.Value);
                pr[1] = new SqlParameter("@MaKH", DBNull.Value);
            }
            tb = con.Excute_ps("ps_laythuedia",pr);
            con.Close();
            refesh();
        }
        public void themmoi()
        {
            string str_MaCD, str_MaKH, str_SoLuongmuon, str_NgayMuon, str_Ngaytra;
            str_MaCD = ((ComboBox)frm.Controls["cbx_Makh"]).Text;
            str_MaKH = ((ComboBox)frm.Controls["cbx_Macd"]).Text;
            str_SoLuongmuon = ((TextBox)frm.Controls["txt_Slgmuon"]).Text;
            str_NgayMuon = ((TextBox)frm.Controls["txt_Ngaymuon"]).Text;
            str_Ngaytra = ((TextBox)frm.Controls["txt_Ngaytra"]).Text;

            SqlParameter[] pr = { new SqlParameter("@MaCD",str_MaCD),
                                  new SqlParameter("@MaKH",str_MaKH),
                                  new SqlParameter("@SoLuongmuon",str_SoLuongmuon),
                                  new SqlParameter("@NgayMuon",str_NgayMuon),
                                  new SqlParameter("@Ngaytra",str_NgayMuon)
                                };
            con.Open();
            tb = con.Excute_ps("ps_themmoi",pr);
            con.Close();
            refesh();
        }
        public void xoa()
        {
            string str_MaCD,str_MaKH;
            str_MaCD = ((ComboBox)frm.Controls["cbx_Macd"]).Text;
            str_MaKH = ((ComboBox)frm.Controls["cbx_Makh"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaCD",str_MaCD),
                                  new SqlParameter("@MaKH",str_MaKH)
                                };
            con.Open();
            tb = con.Excute_ps("ps_xoa", pr);
            con.Close();
            refesh();
        }
        public void sua()
        {
            string str_MaCD,str_MaKH, str_SoLuongmuon,str_NgayMuon,str_Ngaytra;
            str_MaCD = ((ComboBox)frm.Controls["cbx_Macd"]).Text;
            str_MaKH = ((ComboBox)frm.Controls["cbx_Makh"]).Text;
            str_NgayMuon = ((TextBox)frm.Controls["txt_Ngaymuon"]).Text;
            str_Ngaytra = ((TextBox)frm.Controls["txt_Ngaytra"]).Text;
            str_SoLuongmuon = ((TextBox)frm.Controls["txt_Slgmuon"]).Text;

            SqlParameter[] pr = { new SqlParameter("@MaCD",str_MaCD),
                                  new SqlParameter("@MaKH",str_MaKH),
                                  new SqlParameter("@SoLuongmuon",str_SoLuongmuon),
                                  new SqlParameter("@NgayMuon",str_NgayMuon),
                                  new SqlParameter("@Ngaytra",str_Ngaytra)
                                };
            con.Open();
            tb = con.Excute_ps("ps_sua", pr);
            con.Close();
            refesh();
        }
        public void set_combobox1()
           {
                con.Open();
                SqlParameter[] pr = new SqlParameter[1];
                pr[0] = new SqlParameter("@MaCD",DBNull.Value);
                DataTable db = con.Excute_ps("ps_layshopCD", pr);
                ComboBox cb = ((ComboBox)frm.Controls["cbx_Macd"]);
                cb.DataSource = db;
                cb.DisplayMember = "MaCD";
                cb.ValueMember = "MaCD";
           }
        public void set_combobox2()
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter("@MaKH", DBNull.Value);
            DataTable db = con.Excute_ps("ps_laykhachhang", pr);
            ComboBox cb = ((ComboBox)frm.Controls["cbx_Makh"]);
            cb.DataSource = db;
            cb.DisplayMember = "MaKH";
            cb.ValueMember = "MaKH";
        }
    }
}
